<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.events.EventHandler</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.events');
  goog.require('goog.events.EventHandler');
  goog.require('goog.events.EventTarget');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>

<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
<div id="d"></div>

<script>

  var a = document.getElementById('a');
  var b = document.getElementById('b');
  var c = document.getElementById('c');
  var d = document.getElementById('d');

  function tearDown() {
    goog.events.removeAll();
  }

  function testEventHandlerClearsListeners() {
    assertEquals('No event listeners should be registered at the start', 0,
        goog.object.getCount(goog.events.listeners_));

    function tmp() {}

    goog.events.listen(a, 'click', tmp);

    assertEquals('1 listener should be registered now', 1,
        goog.object.getCount(goog.events.listeners_));

    var eh = new goog.events.EventHandler();
    eh.listen(a, 'click');
    eh.listen(a, 'keypress');
    eh.listen(b, 'mouseover');
    eh.listen(c, 'mousedown');
    eh.listen(d, 'click');
    eh.listen(d, 'mousedown');

    assertEquals('7 listeners should be registered now', 7,
        goog.object.getCount(goog.events.listeners_));

    eh.unlisten(d, 'mousedown');

    assertEquals('6 listeners should be registered now', 6,
        goog.object.getCount(goog.events.listeners_));

    eh.dispose();

    assertEquals('1 listener should be left', 1,
        goog.object.getCount(goog.events.listeners_));

    goog.events.unlisten(a, 'click', tmp);

    assertEquals('No event listeners should be registered at the end', 0,
        goog.object.getCount(goog.events.listeners_));
  }

  function testListenArray() {
    var eh = new goog.events.EventHandler();

    eh.listen(a, ['click', 'mousedown', 'mouseup']);

    assertEquals('3 listeners should be registered', 3,
        goog.object.getCount(goog.events.listeners_));

    eh.unlisten(a, ['click', 'mousedown', 'mouseup']);

    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));

    eh.listen(a, ['click', 'mousedown', 'mouseup']);

    assertEquals('3 listeners should be registered', 3,
        goog.object.getCount(goog.events.listeners_));

    eh.removeAll();

    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
  }

  function testListenOnceRemovesListenerWhenFired() {
    var events = [];
    assertEquals('No listeners registered yet', 0,
        goog.object.getCount(goog.events.listeners_));
    var target = new goog.events.EventTarget();
    var eh = new goog.events.EventHandler();
    eh.listenOnce(target, 'click', function(e) { events.push(e); });
    assertEquals('1 listener should be registered', 1,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('click');
    assertEquals('One event should have been dispatched', 1, events.length);
    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('click');
    assertEquals('No event should have been dispatched', 1, events.length);

  }

  function testListenOnceListenerIsCleanedUp() {
    var events = [];
    assertEquals('No listeners registered yet', 0,
        goog.object.getCount(goog.events.listeners_));
    var target = new goog.events.EventTarget();
    var eh = new goog.events.EventHandler();
    eh.listenOnce(target, 'click', function(e) { events.push(e); });
    assertEquals('1 listener should be registered', 1,
        goog.object.getCount(goog.events.listeners_));
    eh.removeAll();
    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('click');
    assertEquals('No event should have been dispatched', 0, events.length);
  }

  function testClearListenersWithListenOnceListenerRemoved() {
    var events = [];
    assertEquals('No listeners registered yet', 0,
        goog.object.getCount(goog.events.listeners_));
    var target = new goog.events.EventTarget();
    var eh = new goog.events.EventHandler();
    eh.listenOnce(target, 'click', function(e) { events.push(e); });
    assertEquals('1 listener should be registered', 1,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('click');
    assertEquals('One event should have been dispatched', 1, events.length);
    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
    eh.removeAll();
    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('click');
    assertEquals('No event should have been dispatched', 1, events.length);
  }

  function testListenOnceArray() {
    var events = [];
    assertEquals('No listeners registered yet', 0,
        goog.object.getCount(goog.events.listeners_));
    var target = new goog.events.EventTarget();
    var eh = new goog.events.EventHandler();
    eh.listenOnce(target, ['click', 'mousedown', 'mouseup'],
    function(e) { events.push(e); });
    assertEquals('3 listeners should be registered', 3,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('click');
    assertEquals('1 event should have been dispatched', 1, events.length);
    assertEquals('Should be a click event', 'click', events[0].type)
    assertEquals('2 listeners should be registered', 2,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('mouseup');
    assertEquals('1 event should have been dispatched', 2, events.length);
    assertEquals('Should be a click event', 'mouseup', events[1].type)
    assertEquals('1 listeners should be registered', 1,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('mousedown');
    assertEquals('1 event should have been dispatched', 3, events.length);
    assertEquals('Should be a click event', 'mousedown', events[2].type)
    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('click');
    assertEquals('No events should have been dispatched', 3, events.length);
  }

  function testListenUnlistenWithObjectHandler() {
    var events = [];
    assertEquals('No listeners registered yet', 0,
        goog.object.getCount(goog.events.listeners_));
    var target = new goog.events.EventTarget();
    var eh = new goog.events.EventHandler();
    var handlerObj = {
      handleEvent: function(e) {
        events.push(e);
      }
    };
    eh.listen(target, 'click', handlerObj);
    assertEquals('1 listener should be registered', 1,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('click');
    assertEquals('1 listener should be registered', 1,
        goog.object.getCount(goog.events.listeners_));
    assertEquals('One event should have been dispatched', 1, events.length);
    eh.unlisten(target, 'click', handlerObj);
    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('click');
    assertEquals('No event should have been dispatched', 1, events.length);
  }

  function testListenOnceWithObjectHandler() {
    var events = [];
    assertEquals('No listeners registered yet', 0,
        goog.object.getCount(goog.events.listeners_));
    var target = new goog.events.EventTarget();
    var eh = new goog.events.EventHandler();
    var handlerObj = {
      handleEvent: function(e) {
        events.push(e);
      }
    };
    eh.listenOnce(target, 'click', handlerObj);
    assertEquals('1 listener should be registered', 1,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('click');
    assertEquals('One event should have been dispatched', 1, events.length);
    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
    target.dispatchEvent('click');
    assertEquals('No event should have been dispatched', 1, events.length);
  }

  function testGetListenerCount() {
    var eh = new goog.events.EventHandler();
    assertEquals('0 listeners registered initially', 0, eh.getListenerCount());
    var target = new goog.events.EventTarget();
    eh.listen(target, 'click', goog.nullFunction, false);
    eh.listen(target, 'click', goog.nullFunction, true);
    assertEquals('2 listeners registered', 2, eh.getListenerCount());
    eh.unlisten(target, 'click', goog.nullFunction, true);
    assertEquals('1 listener removed, 1 left', 1, eh.getListenerCount());
    eh.removeAll();
    assertEquals('all listeners removed', 0, eh.getListenerCount());
  }

</script>
</body>
</html>
